﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

using NUnit.Framework;

using Codeplex.UiAutomationExtension;

namespace UiAutomationExtensionTest
{
    [TestFixture]
    public class UIAutomationExtensionTest
    {
        #region Root

        #region 正常ケース
        [Test]
        public void Root_正常ケース()
        {
            var root = UIAutomationExtension.Root;
            root.IsNotNull();
        }
        #endregion

        #endregion

        #region FromName

        #region FromName(string)

        #region 正常ケース
        [Test, STAThread]
        public void FromName_string_正常ケース()
        {
            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Show();

            try
            {
                UIAutomationExtension.Root.FromName(title).IsNotNull();
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region FromName(string,string)

        #region 正常ケース
        [Test, STAThread]
        public void FromName_string_string_正常ケース()
        {
            var panel = new StackPanel();
            panel.Children.Add(new Button());
            panel.Children.Add(new TextBox());

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = panel;
            win.Show();

            try
            {
                var parent = UIAutomationExtension.Root.FromName(title);
                parent.FromName("", "Button").Current.ClassName.Is("Button");
                parent.FromName("", "TextBox").Current.ClassName.Is("TextBox");
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #endregion

        #region FromId

        #region FromId(string)

        #region 正常ケース
        [Test, STAThread]
        public void FromId_string_正常ケース()
        {
            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = new Button() { Name = "button" };
            win.Show();

            try
            {
                var parent = UIAutomationExtension.Root.FromName(title);
                parent.FromId("button").IsNotNull();
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region FromId(string,string)

        #region 正常ケース
        [Test, STAThread]
        public void FromId_string_string_正常ケース()
        {
            var panel = new StackPanel();
            panel.Children.Add(new Button());
            panel.Children.Add(new TextBox());

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = panel;
            win.Show();

            try
            {
                var parent = UIAutomationExtension.Root.FromName(title);
                parent.FromId("", "Button").Current.ClassName.Is("Button");
                parent.FromId("", "TextBox").Current.ClassName.Is("TextBox");
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #endregion

        #region ElementAt

        #region 正常ケース
        [Test, STAThread]
        public void ElementAt_正常ケース()
        {
            var panel = new StackPanel();
            panel.Children.Add(new Button() { Content = "button1" });
            panel.Children.Add(new Button() { Content = "button2" });

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = panel;
            win.Show();

            try
            {
                var parent = UIAutomationExtension.Root.FromName(title);
                parent.ElementAt(1).Current.Name.Is("button1");
                parent.ElementAt(2).Current.Name.Is("button2");
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region Elements

        #region 正常ケース
        [Test, STAThread]
        public void Elements_正常ケース()
        {
            var panel = new StackPanel();
            panel.Children.Add(new Button() { Content = "button1" });
            panel.Children.Add(new Button() { Content = "button2" });
            panel.Children.Add(new Button() { Content = "button3" });

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = panel;
            win.Show();
            
            try
            {
                var parent = UIAutomationExtension.Root.FromName(title);
                parent.Elements().ElementAt(1).Current.Name.Is("button1");
                parent.Elements().ElementAt(2).Current.Name.Is("button2");
                parent.Elements().ElementAt(3).Current.Name.Is("button3");
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region AsValue

        #region 正常ケース
        [Test, STAThread]
        public void AsValue_正常ケース()
        {
            var text = new TextBox();
            text.Name = "text";

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = text;
            win.Show();

            try
            {
                var textBox = UIAutomationExtension.Root.FromName(title).FromId("text").AsValue();
                textBox.IsNotNull();

                textBox.SetValue("foo");
                text.Text.Is("foo");

                text.Text = "bar";
                textBox.Current.Value.Is("bar");
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region AsInvoke

        #region 正常ケース
        [Test, STAThread]
        public void AsInvoke_正常ケース()
        {
            var called = false;

            var button = new Button();
            button.Name = "button";
            button.Click += (obj, e) => called = true;

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = button;
            win.Show();

            try
            {
                var btn = UIAutomationExtension.Root.FromName(title).FromId("button").AsInvoke();
                btn.IsNotNull();

                btn.Invoke();

                //called.Is(true);
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region AsListItemCollection

        #region 正常ケース
        [Test, STAThread]
        public void AsListItemCollection_正常ケース()
        {
            var list = new ListBox();
            list.Name = "list";
            list.ItemsSource = new[] { "foo", "bar", "hoge" };

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = list;
            win.Show();

            try
            {
                var l = UIAutomationExtension.Root.FromName(title).FromId("list").AsListItemCollection();
                l.IsNotNull();
                l[0].Current.Name.Is("foo");
                l[1].Current.Name.Is("bar");
                l[2].Current.Name.Is("hoge");
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region AsSelectionItem

        #region 正常ケース
        [Test, STAThread]
        public void AsSelectionItem_正常ケース()
        {
            var list = new ListBox();
            list.Name = "list";
            list.ItemsSource = new[] { "foo", "bar", "hoge" };

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = list;
            win.Show();

            try
            {
                var l = UIAutomationExtension.Root.FromName(title).FromId("list").AsListItemCollection();
                l.IsNotNull();

                l[0].AsSelectionItem().Current.IsSelected.Is(false);

                list.SelectedIndex = 0;

                l[0].AsSelectionItem().Current.IsSelected.Is(true);

                l[1].AsSelectionItem().Select();

                list.SelectedIndex.Is(1);

                list.SelectionMode = SelectionMode.Extended;

                l[0].AsSelectionItem().AddToSelection();

                (list.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem).IsSelected.Is(true);
                (list.ItemContainerGenerator.ContainerFromIndex(1) as ListBoxItem).IsSelected.Is(true);
                (list.ItemContainerGenerator.ContainerFromIndex(2) as ListBoxItem).IsSelected.Is(false);
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region AsSelectionItemCollection

        #region 正常ケース
        [Test, STAThread]
        public void AsSelectionItemCollection_正常ケース()
        {
            var list = new ListBox();
            list.Name = "list";
            list.SelectionMode = SelectionMode.Extended;
            list.ItemsSource = new[] { "foo", "bar", "hoge" };

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = list;
            win.Show();

            try
            {
                var l = UIAutomationExtension.Root.FromName(title).FromId("list").AsSelectionItemCollection();
                l.IsNotNull();

                l.ElementAt(0).Current.IsSelected.Is(false);
                l.ElementAt(1).Current.IsSelected.Is(false);
                l.ElementAt(2).Current.IsSelected.Is(false);

                l.ElementAt(0).Select();
                l.ElementAt(0).Current.IsSelected.Is(true);
                l.ElementAt(1).Current.IsSelected.Is(false);
                l.ElementAt(2).Current.IsSelected.Is(false);

                l.ElementAt(1).Select();
                l.ElementAt(0).Current.IsSelected.Is(false);
                l.ElementAt(1).Current.IsSelected.Is(true);
                l.ElementAt(2).Current.IsSelected.Is(false);

                l.ElementAt(2).AddToSelection();
                l.ElementAt(0).Current.IsSelected.Is(false);
                l.ElementAt(1).Current.IsSelected.Is(true);
                l.ElementAt(2).Current.IsSelected.Is(true);
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion

        #region AsExpandCollapse

        #region 正常ケース
        [Test, STAThread]
        public void AsExpandCollapse_正常ケース()
        {
            var node1 = new TreeViewItem();
            node1.Header = "node1";

            var node2 = new TreeViewItem();
            node2.Header = "node2";

            var root = new TreeViewItem();
            root.Header = "root";
            root.Items.Add(node1);
            root.Items.Add(node2);

            var tree = new TreeView();
            tree.Name = "tree";
            tree.Items.Add(root);

            var title = "UIAutomationExtensionTest";
            var win = new Window();
            win.Title = title;
            win.Content = tree;
            win.Show();

            try
            {
                var item = UIAutomationExtension.Root.FromName(title).FromId("tree").FromName("root").AsExpandCollapse();

                root.IsExpanded.Is(false);

                item.Expand();

                root.IsExpanded.Is(true);

                item.Collapse();

                root.IsExpanded.Is(false);
            }
            finally
            {
                win.Close();
            }
        }
        #endregion

        #endregion
    }
}
